using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using Sensor.SerialPorts;
using Sensor.SerialPorts.Enums;

namespace Sensor.UI.ViewModels;

/// <summary>
/// 串口配置视图模型
/// 负责管理串口配置选项的提供和管理
/// </summary>
public partial class SerialPortConfigViewModel : ViewModelBase
{
    #region 私有字段

    /// <summary>
    /// 获取或设置可用串口名称列表
    /// </summary>
    [ObservableProperty]
    private ObservableCollection<string> _port = [];

    /// <summary>
    /// 获取或设置可用波特率列表
    /// </summary>
    [ObservableProperty]
    private ObservableCollection<int> _baudRate = [];

    /// <summary>
    /// 获取或设置可用校验位列表
    /// </summary>
    [ObservableProperty]
    private ObservableCollection<ParityBit> _parityBit = [];

    /// <summary>
    /// 获取或设置可用停止位列表
    /// </summary>
    [ObservableProperty]
    private ObservableCollection<StopBit> _stopBit = [];

    /// <summary>
    /// 获取或设置可用数据位列表
    /// </summary>
    [ObservableProperty]
    private ObservableCollection<int> _dataBit = [];

    #endregion

    #region 公共属性
    // 当前所有属性都在私有字段中使用ObservableProperty特性定义，没有额外的公共属性
    #endregion

    #region 构造函数

    /// <summary>
    /// 初始化 SerialPortConfigViewModel 类的新实例
    /// </summary>
    public SerialPortConfigViewModel()
    {
        InitializePortOptions();
        InitializeBaudRateOptions();
        InitializeParityBitOptions();
        InitializeStopBitOptions();
        InitializeDataBitOptions();
    }

    #endregion

    #region 公共方法

    /// <summary>
    /// 刷新可用串口列表
    /// 在串口设备连接或断开时调用此方法更新可用串口列表
    /// </summary>
    public void RefreshPortList()
    {
        Port.Clear();
        var portNames = SerialPortHelp.GetPortNames();
        
        foreach (var portName in portNames)
        {
            Port.Add(portName);
        }
    }

    #endregion

    #region 私有方法

    /// <summary>
    /// 初始化串口选项
    /// </summary>
    private void InitializePortOptions()
    {
        var portNames = SerialPortHelp.GetPortNames();
        
        foreach (var portName in portNames)
        {
            Port.Add(portName);
        }
    }

    /// <summary>
    /// 初始化波特率选项
    /// </summary>
    private void InitializeBaudRateOptions()
    {
        var baudRates = new[] { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200 };
        
        foreach (var baudRate in baudRates)
        {
            BaudRate.Add(baudRate);
        }
    }

    /// <summary>
    /// 初始化校验位选项
    /// </summary>
    private void InitializeParityBitOptions()
    {
        var parityBits = Enum.GetValues<ParityBit>();
        
        foreach (var parityBit in parityBits)
        {
            ParityBit.Add(parityBit);
        }
    }

    /// <summary>
    /// 初始化停止位选项
    /// </summary>
    private void InitializeStopBitOptions()
    {
        var stopBits = Enum.GetValues<StopBit>();
        
        foreach (var stopBit in stopBits)
        {
            StopBit.Add(stopBit);
        }
    }

    /// <summary>
    /// 初始化数据位选项
    /// </summary>
    private void InitializeDataBitOptions()
    {
        var dataBits = new[] { 6, 7, 8 };
        
        foreach (var dataBit in dataBits)
        {
            DataBit.Add(dataBit);
        }
    }

    #endregion
}